﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace GypeLaunch.Wrapper
{
    public struct IMAGE_DOS_HEADER
    {
        public UInt16 magic;                     // Magic number
        public UInt16 cblp;                      // Bytes on last page of file
        public UInt16 cp;                        // Pages in file
        public UInt16 crlc;                      // Relocations
        public UInt16 cparhdr;                   // Size of header in paragraphs
        public UInt16 minalloc;                  // Minimum extra paragraphs needed
        public UInt16 maxalloc;                  // Maximum extra paragraphs needed
        public UInt16 ss;                        // Initial (relative) SS value
        public UInt16 sp;                        // Initial SP value
        public UInt16 csum;                      // Checksum
        public UInt16 ip;                        // Initial IP value
        public UInt16 cs;                        // Initial (relative) CS value
        public UInt16 lfarlc;                    // File address of relocation table
        public UInt16 ovno;                      // Overlay number
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
        public UInt16[] res;                     // Reserved words
        public UInt16 oemid;                     // OEM identifier (for e_oeminfo)
        public UInt16 oeminfo;                   // OEM information; e_oemid specific
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=10)]
        public UInt16[] res2;                    // Reserved words
        public UInt32 lfanew;                    // File address of new exe header
    }

    public struct IMAGE_FILE_HEADER
    {
        public UInt16 Machine;
        public UInt16 NumberOfSections;
        public UInt32 TimeDateStamp;
        public UInt32 PointerToSymbolTable;
        public UInt32 NumberOfSymbols;
        public UInt16 SizeOfOptionalHeader;
        public UInt16 Characteristics;
    }

    public struct IMAGE_DATA_DIRECTORY
    {
        public UInt32 VirtualAddress;
        public UInt32 Size;
    }

    public struct IMAGE_OPTIONAL_HEADER32
    {
        //
        // Standard fields.
        //

        public UInt16 Magic;
        public byte MajorLinkerVersion;
        public byte MinorLinkerVersion;
        public UInt32 SizeOfCode;
        public UInt32 SizeOfInitializedData;
        public UInt32 SizeOfUninitializedData;
        public UInt32 AddressOfEntryPoint;
        public UInt32 BaseOfCode;
        public UInt32 BaseOfData;

        //
        // NT additional fields.
        //

        public UInt32 ImageBase;
        public UInt32 SectionAlignment;
        public UInt32 FileAlignment;
        public UInt16 MajorOperatingSystemVersion;
        public UInt16 MinorOperatingSystemVersion;
        public UInt16 MajorImageVersion;
        public UInt16 MinorImageVersion;
        public UInt16 MajorSubsystemVersion;
        public UInt16 MinorSubsystemVersion;
        public UInt32 Win32VersionValue;
        public UInt32 SizeOfImage;
        public UInt32 SizeOfHeaders;
        public UInt32 CheckSum;
        public UInt16 Subsystem;
        public UInt16 DllCharacteristics;
        public UInt32 SizeOfStackReserve;
        public UInt32 SizeOfStackCommit;
        public UInt32 SizeOfHeapReserve;
        public UInt32 SizeOfHeapCommit;
        public UInt32 LoaderFlags;
        public UInt32 NumberOfRvaAndSizes;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public IMAGE_DATA_DIRECTORY[] DataDirectory;
    }

    public struct IMAGE_OPTIONAL_HEADER64
    {
        public UInt16 Magic;
        public byte MajorLinkerVersion;
        public byte MinorLinkerVersion;
        public UInt32 SizeOfCode;
        public UInt32 SizeOfInitializedData;
        public UInt32 SizeOfUninitializedData;
        public UInt32 AddressOfEntryPoint;
        public UInt32 BaseOfCode;
        public UInt64 ImageBase;
        public UInt32 SectionAlignment;
        public UInt32 FileAlignment;
        public UInt16 MajorOperatingSystemVersion;
        public UInt16 MinorOperatingSystemVersion;
        public UInt16 MajorImageVersion;
        public UInt16 MinorImageVersion;
        public UInt16 MajorSubsystemVersion;
        public UInt16 MinorSubsystemVersion;
        public UInt32 Win32VersionValue;
        public UInt32 SizeOfImage;
        public UInt32 SizeOfHeaders;
        public UInt32 CheckSum;
        public UInt16 Subsystem;
        public UInt16 DllCharacteristics;
        public UInt64 SizeOfStackReserve;
        public UInt64 SizeOfStackCommit;
        public UInt64 SizeOfHeapReserve;
        public UInt64 SizeOfHeapCommit;
        public UInt32 LoaderFlags;
        public UInt32 NumberOfRvaAndSizes;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public IMAGE_DATA_DIRECTORY[] DataDirectory;
    }

    public struct IMAGE_NT_HEADERS32
    {
        public UInt32 Signature;
        public IMAGE_FILE_HEADER FileHeader;
        public IMAGE_OPTIONAL_HEADER32 OptionalHeader;
    }

    public struct IMAGE_NT_HEADERS64
    {
        public UInt32 Signature;
        public IMAGE_FILE_HEADER FileHeader;
        public IMAGE_OPTIONAL_HEADER64 OptionalHeader;
    }

    public struct IMAGE_SECTION_HEADER
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[]   Name;
        public UInt32   Misc;
        /*{
                DWORD   PhysicalAddress;
                DWORD   VirtualSize;
        } */
        public UInt32   VirtualAddress;
        public UInt32   SizeOfRawData;
        public UInt32   PointerToRawData;
        public UInt32   PointerToRelocations;
        public UInt32   PointerToLinenumbers;
        public UInt16   NumberOfRelocations;
        public UInt16   NumberOfLinenumbers;
        public UInt32   Characteristics;
    }

    public struct IMAGE_IMPORT_DESCRIPTOR
    {
        public UInt32 Characteristics;
        public UInt32 TimeDateStamp;
        public UInt32 ForwarderChain;
        public UInt32 Name;
        public UInt32 FirstThunk;
    }

    public struct IMAGE_THUNK_DATA32
    {
        public UInt32 u1;
    }

    public struct IMAGE_THUNK_DATA64
    {
        public UInt64 u1;
    }

    public struct IMAGE_IMPORT_BY_NAME
    {
        public UInt16 Hint;
        [MarshalAs(UnmanagedType.ByValArray)]
        public byte Name;
    }
}
